package com.imooc.mall.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.imooc.mall.exception.ImoocMallException;
import com.imooc.mall.exception.ImoocMallExceptionEnum;
import com.imooc.mall.model.dao.CategoryMapper;
import com.imooc.mall.model.pojo.Category;
import com.imooc.mall.model.request.AddCategoryReq;
import com.imooc.mall.model.request.UpdateCategoryReq;
import com.imooc.mall.service.CategoryService;
import com.imooc.mall.model.vo.CategoryVO;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service("categoryService")
public class CategoryServiceImpl implements CategoryService {
    @Resource
    private CategoryMapper categoryMapper;

    /**
     * 添加目录分类
     *
     * @param addCategoryReq
     */
    @Override
    public void add(AddCategoryReq addCategoryReq) {
        Category category = new Category();
        BeanUtils.copyProperties(addCategoryReq, category);
        // 查询是否存在重名
        Category categoryOld = categoryMapper.selectByName(addCategoryReq.getName());
        if (categoryOld != null) {
            throw new ImoocMallException(ImoocMallExceptionEnum.NAME_EXISTED);
        }
        int count = categoryMapper.insertSelective(category);
        if (count == 0) {
            throw new ImoocMallException(ImoocMallExceptionEnum.CREATE_FAILED);
        }
    }

    /**
     * 更新商品分类
     *
     * @param updateCategoryReq
     */
    @Override
    public void update(UpdateCategoryReq updateCategoryReq) {
        // 查询是否重名
        Category categoryOld = categoryMapper.selectByName(updateCategoryReq.getName());
        if (categoryOld != null && !categoryOld.getId().equals(updateCategoryReq.getId())) {
            throw new ImoocMallException(ImoocMallExceptionEnum.NAME_EXISTED);
        }
        Category category = new Category();
        BeanUtils.copyProperties(updateCategoryReq, category);
        int count = categoryMapper.updateByPrimaryKeySelective(category);
        if (count == 0) {
            throw new ImoocMallException(ImoocMallExceptionEnum.UPDATE_FAILED);
        }
    }

    /**
     * 删除商品分类
     *
     * @param id 商品分类id
     */
    @Override
    public void delete(Integer id) {
        Category categoryOld = categoryMapper.selectByPrimaryKey(id);
        if (categoryOld == null) {
            // 查不到记录，删除失败
            throw new ImoocMallException(ImoocMallExceptionEnum.DELETE_FAILED);
        }
        int count = categoryMapper.deleteByPrimaryKey(id);
        if (count == 0) {
            throw new ImoocMallException(ImoocMallExceptionEnum.DELETE_FAILED);
        }
    }

    /**
     * 分页功能
     *
     * @param pageNum
     * @param pageSize
     * @return
     */
    @Override
    public PageInfo listForAdmin(Integer pageNum, Integer pageSize) {
        //调用分页功能，将查询到的数据按设置分页。把list包裹在内部，而不是直接返回
        PageHelper.startPage(pageNum, pageSize, "type,order_num");
        // 查询
        List<Category> categoryList = categoryMapper.selectList();
        // PageInfo是返回给前端的类型
        PageInfo pageInfo = new PageInfo(categoryList);
        return pageInfo;
    }

    /**
     * 前台目录列表
     *
     * @return
     */
    @Override
    public List<CategoryVO> listCategoryForCustomer(Integer parentId) {
        ArrayList<CategoryVO> categoryVOList = new ArrayList<>();
        // 对于一级目录而言，父目录为零
        recursivelyFindCategories(categoryVOList, parentId);
        return categoryVOList;
    }

    // 实现递归
    private void recursivelyFindCategories(List<CategoryVO> categoryVOList, Integer parentId) {
        // 递归获取所有子类别，并组合成为一个"目录树"
        List<Category> categoriesList = categoryMapper.selectCategoriesByParentId(parentId);
        // 使用CollectionUtils判断是否存在
        if (!CollectionUtils.isEmpty(categoriesList)) {
            // 存在
            for (Category category : categoriesList) {
                CategoryVO categoryVO = new CategoryVO();
                // 将category数据拷贝到categoryVO
                BeanUtils.copyProperties(category, categoryVO);
                // 将categoryVO传入参数categoryVOList中
                categoryVOList.add(categoryVO);
                recursivelyFindCategories(categoryVO.getChildCategory(), categoryVO.getId());
            }
        }
    }
}
